package com.wuyou.mid;

/**
 * 1669. 合并两个链表
 */
public class MergeInBetween {


    public static void main(String[] args) {
        ListNode list1 = new ListNode(0);
        list1.next = new ListNode(1);
        list1.next.next = new ListNode(2);
        list1.next.next.next = new ListNode(3);
        list1.next.next.next.next = new ListNode(4);
        list1.next.next.next.next.next = new ListNode(5);

        ListNode list2 = new ListNode(1000000);
        list2.next = new ListNode(1000001);
        list2.next.next = new ListNode(1000002);

        System.out.println(mergeInBetween(list1, 3, 4, list2));
    }


    public static ListNode mergeInBetween(ListNode list1, int a, int b, ListNode list2) {
        int index = 1;
        ListNode head = list1;
        while (list1.next != null) {
            if (index == a) {
                ListNode temp = list1.next;
                while (index <= b){
                    temp = temp.next;
                    index ++;
                }
                list1.next = list2;
                while (list2.next != null) {
                    list2 = list2.next;
                }
                list2.next = temp;
            }else {
                index++;
                list1 = list1.next;
            }
        }
        return head;

    }
}
